﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;

namespace Logistics.DataManage.OracleDb
{
    [Obsolete("本类已经过时，请使用OracleHandler类型")]
    public class OracleMSHandler :IDisposable,IOraHandler
    {
        private String connectString;

        public OracleMSHandler(String _connString)
        {
            this.connectString = _connString;
        }

        public int ExecuteNonQuery(string sql,params IDataParameter[] paras)
        {
            using (OracleConnection connection = new OracleConnection(connectString))
            {
                try
                {
                    connection.Open();
                    OracleCommand sqlCMD = connection.CreateCommand();
                    sqlCMD.CommandText = sql; 
                    if(paras!=null && paras.Length>0)
                       foreach(var para in paras)
                           sqlCMD.Parameters.Add(para);
                    int i = sqlCMD.ExecuteNonQuery();                                 //执行sql语句
                    connection.Close();
                    return i;
                }
                catch(Exception)
                {
                    throw;                                
                }
                finally
                {
                   connection.Close();
                }
            }
        }

        public DataSet ExecuteQuery(string sql, params IDataParameter[] paras)
        {
            using (OracleConnection connection = new OracleConnection(connectString))       //建立数据连接对象
            {
                try
                {
                    connection.Open();
                    System.Data.OracleClient.OracleCommand sqlCMD = connection.CreateCommand();
                    sqlCMD.CommandText = sql;
                    if (paras != null && paras.Length > 0)
                        foreach (var para in paras)
                            sqlCMD.Parameters.Add(para);
                    DataSet ds = new DataSet();
                    using (OracleDataAdapter myAdapter = new OracleDataAdapter(sqlCMD))
                    {
                        myAdapter.Fill(ds);
                    }
                    connection.Close();
                    return ds;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        public Object ExecuteScalar(string sql, params IDataParameter[] paras)
        {
            using (OracleConnection connection = new OracleConnection(connectString)) 
            {
                try
                {
                    connection.Open();
                    System.Data.OracleClient.OracleCommand sqlCMD = connection.CreateCommand();
                    sqlCMD.CommandText = sql;
                    if (paras != null && paras.Length > 0)
                        foreach (var para in paras)
                            sqlCMD.Parameters.Add(para);
                    Object obj = sqlCMD.ExecuteScalar();
                    connection.Close();
                    return obj;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        public void Dispose()
        {
            
        }
    }
}
